package com.xhd.pagination;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Vector;

import com.xhd.base.util.xBaseObj;
import com.xhd.cache.CacheMgr;
import com.xhd.cache.UseCache;
import com.xhd.db.BizObjProxy;
import com.xhd.exception.IllegalLogicException;
import com.xhd.exception.XException;
import com.xhd.server.ServerConfig;
import com.xhd.util.DataRelation;

public class PageCache implements UseCache {
	private static PageCache jm16 = null;
	private static HashMap jm459 = new HashMap();
	private static HashMap jm460 = new HashMap();
	private static int jm254 = 1;
	private static int jm461 = 200;
	private static int jm462 = 300000;

	private PageCache() {
		jm461 = ServerConfig.getInt("page-cache-limit", jm461);
		jm462 = ServerConfig.getInt("page-cache-timeout", jm462);
		CacheMgr.registerCache(this);
	}

	public static synchronized PageCache getInstance() {
		if (jm16 == null)
			jm16 = new PageCache();
		return jm16;
	}

	private static synchronized int jm458() {
		return (jm254++);
	}

	public int cacheData(xBaseObj paramxBaseObj, String paramString,
			int paramInt1, int paramInt2, int paramInt3) throws XException {
		String str1 = paramxBaseObj.getClassName()
				+ paramxBaseObj.getUserInfo().getSessionId() + paramString;
		String str2 = (String) jm460.get(str1);
		if (str2 != null) {
			jm459.remove(str2);
			jm460.remove(str1);
		}
		BizObjProxy localBizObjProxy = new BizObjProxy(paramxBaseObj);
		DataRelation localDataRelation = (DataRelation) localBizObjProxy
				.getValue(paramString);
		if (localDataRelation == null)
			return 0;
		Vector localVector1 = localDataRelation.getRelationData();
		int i = (localVector1 == null) ? 0 : localVector1.size();
		if (i < 1)
			return 0;
		CacheItem localCacheItem = new CacheItem(null);
		localCacheItem.jm21 = str1;
		localCacheItem.jm144 = paramxBaseObj.toHashtable();
		localCacheItem.jm144.remove("pagination");
		localCacheItem.jm145 = paramString.toLowerCase();
		if (paramInt3 > 0)
			paramInt1 -= paramInt3;
		if ((paramInt1 >= i) || (paramInt1 < 0))
			throw new IllegalLogicException("Invail index: " + paramInt1);
		int j;
		if (paramInt2 < 1) {
			j = i;
		} else {
			j = paramInt1 + paramInt2;
			if (j > i)
				j = i;
		}
		if ((paramInt1 == 0) && (j == i)) {
			localCacheItem.jm146 = new Vector(i);
			localCacheItem.jm146.addAll(localVector1);
		} else {
			Vector localVector2 = new Vector(j - paramInt1);
			for (int l = paramInt1; l < j; ++l)
				localVector2.add(localVector1.get(l));
			localCacheItem.jm146 = localVector2;
		}
		if (paramInt3 > 0) {
			paramInt1 += paramInt3;
			j += paramInt3;
		}
		localCacheItem.jm147 = paramInt1;
		localCacheItem.jm148 = j;
		int k = jm458();
		localCacheItem.jm142 = k;
		localCacheItem.jm143 = System.currentTimeMillis();
		str2 = String.valueOf(k);
		jm459.put(str2, localCacheItem);
		jm460.put(str1, str2);
		return k;
	}

	public boolean getData(xBaseObj paramxBaseObj) throws XException {
		DataPaging localDataPaging = paramxBaseObj.getPagination();
		if (localDataPaging.getCacheId() < 1)
			return false;
		CacheItem localCacheItem = (CacheItem) jm459.get(String
				.valueOf(localDataPaging.getCacheId()));
		if (localCacheItem == null)
			return false;
		int i = localDataPaging.getPageCount();
		int j = localDataPaging.getPageSize();
		int k = localDataPaging.getCurrPage();
		if ((k < 0) || (k >= i))
			throw new IllegalLogicException("Invaild page: "
					+ localDataPaging.getCurrPage());
		int l = k * j;
		int i1 = l + j;
		if (k == i - 1)
			i1 = localDataPaging.getCount();
		if ((l < localCacheItem.jm147) || (i1 > localCacheItem.jm148))
			return false;
		int i2 = localCacheItem.jm147;
		if (i2 > 0) {
			l -= i2;
			i1 -= i2;
		}
		Vector localVector1 = new Vector(j);
		Vector localVector2 = localCacheItem.jm146;
		for (int i3 = l; i3 < i1; ++i3)
			localVector1.add(localVector2.get(i3));
		localCacheItem.jm144.put(localCacheItem.jm145, localVector1);
		paramxBaseObj.toObject(localCacheItem.jm144);
		return true;
	}

	public void checkTimeOut() {
		if (jm459.size() < 1)
			return;
		Vector localVector = new Vector();
		localVector.addAll(jm459.values());
		long l = System.currentTimeMillis() - jm462;
		for (int i = localVector.size() - 1; i >= 0; --i) {
			CacheItem localCacheItem = (CacheItem) localVector.get(i);
			if (localCacheItem.jm143 >= l)
				continue;
			jm459.remove(String.valueOf(localCacheItem.jm142));
			jm460.remove(localCacheItem.jm21);
		}
	}

	public boolean clearCache() {
		jm459.clear();
		jm460.clear();
		return true;
	}

	public int cacheSize() {
		return jm459.size();
	}

	public Object cacheData() {
		return jm459;
	}

	private class CacheItem {
		int jm142;
		String jm21;
		long jm143;
		Hashtable jm144;
		String jm145;
		Vector jm146;
		int jm147;
		int jm148;
		int jm149;
		final PageCache jm19;

		private CacheItem() {
			this.jm19 = new PageCache();
			this.jm149 = 0;
		}

		CacheItem(CacheItem paramCacheItem) {
			this.jm19 = new PageCache();
		}
	}
}